/**
 * Fetch data for Zone 4
 */
//Class Chart
//Column
Ext.define('Cps.chart.Column', {
	extend : 'Ext.chart.Chart',
	alias : 'widget.chartColumn',
	width: '100%',
	animate: true,
	insetPadding: 40,
	axes: [
        {
            type: 'Numeric',
            position: 'left',
            fields: ['value'],
            label: {
                renderer: Ext.util.Format.numberRenderer('0,0')
            },
            title: 'Value',
            grid: true,
        },
        {
            type: 'Category',
            position: 'bottom',
            fields: ['obj']
        }
    ],
    series: [
        {
            type: 'column',
            axis: 'left',
            highlight: true,
            label: {
              display: 'insideEnd',
				'text-anchor': 'middle',
                field: 'value',
                renderer: Ext.util.Format.numberRenderer('0'),
                orientation: 'vertical',
				font: '12px Helvetica, sans-serif',
                color: '#333'
            },
            xField: 'obj',
            yField: 'value'
        }
    ]
});
//Line
Ext.define('Cps.chart.Line',{
	extend : 'Ext.chart.Chart',
	alias : 'widget.chartLine',
	width: '100%',
	animate: true,
	insetPadding: 40,
	axes: [
        {
            type: 'Numeric',
            position: 'left',
            fields: ['value'],
            label: {
                renderer: Ext.util.Format.numberRenderer('0,0')
            },
            title: 'Value',
            grid: true
        },
        {
            type: 'Category',
            position: 'bottom',
            fields: ['obj']
        }
    ],
    series: [
        {
            type: 'line',
            highlight: {
                size: 7,
                radius: 7
            },
            axis: 'left',
            xField: 'obj',
            yField: 'value',
            markerConfig: {
                type: 'cross',
                size: 4,
                radius: 4,
                'stroke-width': 0
            }
        }
    ]
});
//Pie
Ext.define('Cps.chart.Pie',{
	extend : 'Ext.chart.Chart',
	alias : 'widget.chartPie',
	width: '100%',
	insetPadding: 30,
	theme: 'Base:gradients',
	legend: {
		position: 'right'
	},
    series: [{
        type: 'pie',
        field: 'value',
        showInLegend: true,
        tips: {
            trackMouse: true,
            width: 140,
            height: 28,
            renderer: function(storeItem, item) {
                var total = 0;
                Ext.getCmp('chartCtn').items.items[0].getStore().each(function(rec) {
                    total += rec.get('value');
                });
                this.setTitle(storeItem.get('obj') + ': ' + Math.round(storeItem.get('value') / total * 100) + '%');
            }
        },
        highlight: {
            segment: {
                margin: 20
            }
        },
        label: {
            field: 'obj',
            display: 'rotate',
            contrast: true,
            font: '16px Arial'
        }
    }]
});
//Bar
Ext.define('Cps.chart.Bar',{
	extend : 'Ext.chart.Chart',
	alias : 'widget.chartBar',
	width: '100%',
	animation: {
		easing: 'easeOut',
		duration: 500
	},
	insetPadding: 40,
	theme: 'Sky',
	interactions: 'itemhighlight',
	axes: [{
        type: 'Numeric',
        position: 'bottom',
        fields: ['value'],
        label: {
            renderer: Ext.util.Format.numberRenderer('0,0')
        },
        title: 'Value',
        grid: true
    }, {
        type: 'Category',
        position: 'left',
        fields: ['obj']
    }],
    series: [{
        type: 'bar',
        axis: 'bottom',
        highlight: true,
        tips: {
          trackMouse: true,
          width: 140,
          height: 28,
          renderer: function(storeItem, item) {
            this.setTitle(storeItem.get('obj') + ': ' + storeItem.get('value'));
          }
        },
        label: {
          display: 'insideEnd',
            field: 'value',
            renderer: Ext.util.Format.numberRenderer('0'),
            orientation: 'horizontal',
            color: '#333',
            'text-anchor': 'middle'
        },
        xField: 'obj',
        yField: 'value'
    }]
});
//Area
Ext.define('Cps.chart.Area',{
	extend : 'Ext.chart.Chart',
	alias : 'widget.chartArea',
	width: '100%',
	insetPadding: 40,
	axes: [
        {
            type: 'Numeric',
            position: 'left',
            fields: ['value'],
            title: 'Value',
            grid: {
                odd: {
                    opacity: 1,
                    fill: '#ddd',
                    stroke: '#bbb',
                    'stroke-width': 1
                }
            },
            adjustMinimumByMajorUnit: 0
        },
        {
            type: 'Category',
            position: 'bottom',
            fields: ['obj'],
            grid: true,
            label: {
                rotate: {
                    degrees: 315
                }
            }
        }
    ],
    series: [{
        type: 'area',
        highlight: false,
        axis: 'left',
        xField: 'obj',
        yField: ['value'],
        style: {
            opacity: 0.93
        }
    }]
});
//Donut
Ext.define('Cps.chart.Donut',{
	extend : 'Ext.chart.Chart',
	alias : 'widget.chartDonut',
	width: '100%',
	insetPadding: 30,
	theme: 'Base:gradients',
	legend: {
		position: 'right'
	},
    series: [{
        type: 'pie',
        field: 'value',
		donut: 35,
        showInLegend: true,
        tips: {
            trackMouse: true,
            width: 140,
            height: 28,
            renderer: function(storeItem, item) {
                var total = 0;
                Ext.getCmp('chartCtn').items.items[0].getStore().each(function(rec) {
                    total += rec.get('value');
                });
                this.setTitle(storeItem.get('obj') + ': ' + Math.round(storeItem.get('value') / total * 100) + '%');
            }
        },
        highlight: {
            segment: {
                margin: 20
            }
        },
        label: {
            field: 'obj',
            display: 'rotate',
            contrast: true,
            font: '16px Arial'
        }
    }]
});
//Radial
Ext.define('Cps.chart.Radial',{
	extend : 'Ext.chart.Chart',
	alias : 'widget.chartRadial',
	width: '100%',
	insetPadding: 40,
    axes: [{
        type: 'Radial',
        position: 'radial',
        label: {
            display: true
        }
    }],
    series: [{
        type: 'radar',
        xField: 'obj',
        yField: 'value',
        showInLegend: true,
        showMarkers: true,
        markerConfig: {
            radius: 5,
            size: 5
        },
        style: {
            'stroke-width': 2,
            fill: 'none'
        }
    }]
});
//Scatter
Ext.define('Cps.chart.Scatter',{
	extend : 'Ext.chart.Chart',
	alias : 'widget.chartScatter',
	width: '100%',
	store: Ext.create('Ext.data.JsonStore', {
		fields: ['x', 'y' ],
		data: [
			{ x: 5, y: 20 },
			{ x: 480, y: 90 },
			{ x: 250, y: 50 },
			{ x: 100, y: 33 },
			{ x: 330, y: 95 },
			{ x: 410, y: 12 },
			{ x: 475, y: 44 },
			{ x: 25, y: 67 },
			{ x: 85, y: 21 },
			{ x: 220, y: 88 },
			{ x: 320, y: 79 },
			{ x: 270, y: 32 }
		]
	}),
	insetPadding: 40,
	interactions: 'itemhighlight',
	axes: [{
		type: 'numeric',
		position: 'bottom',
		fields: ['x'],
		grid: true
	}, {
		type: 'numeric',
		position: 'left',
		fields: ['y'],
		grid: true
	}],
	series: [{
		type: 'scatter',
		xField: 'x',
		yField: 'y',
		marker: {
			radius: 4
		},
		highlight: {
			fillStyle: 'yellow',
			radius: 7,
			lineWidth: 2
		},
		label: {
			field: 'x',
			display: 'over',
			font: '14px',
			renderer: function(text, label, labelCfg, data, index) {
				var record = data.store.getAt(index);
				return record.get('x') + ',' + record.get('y');
			}
		}
	}]
});
//Zone 4
Ext.define('Cps.view.dashboard.Zone4', {
	
	config: {
		params: {},
        panel: null //the panel to display
	},
	
	constructor: function(config) {
		if(config)
			this.initConfig(config);
	},
	
	redererValue: function(data, meta, rec, row, col){
		if(!data)
			data = '';
		else {
			str = data >= 0 ? Ext.util.Format.number(data, '0,000.00') : ('-' + Ext.util.Format.number(-data, '0,000.00'));
			str = str.replace(/[,]/g,"'");
			data = (data > 0) ? ('<div style="text-align: right;color:blue">' + str + '</div>') : ('<div style="text-align: right;color:red">' + str + '</div>');
		}
		return data;
	},
	
	redererValue1: function(data, meta, rec, row, col){
		if(!data)
			data = '';
		else {
			str = data >= 0 ? Ext.util.Format.number(data, '0,000.00') : ('-' + Ext.util.Format.number(-data, '0,000.00'));
			str = str.replace(/[,]/g,"'");
			data = (data > 0) ? ('<div style="text-align: right">' + str + '</div>') : ('<div style="text-align: right">' + str + '</div>');
		}
		return data;
	},
	
	redererValue2: function(data, meta, rec, row, col){
		if(!data)
			data = '';
		else {
			str = data >= 0 ? Ext.util.Format.number(data, '0,000.00') : ('-' + Ext.util.Format.number(-data, '0,000.00'));
			str = str.replace(/[,]/g,"'");
			data = (data > 0) ? ('<div style="text-align: right;color:blue">' + str + '</div>') : ('<div style="text-align: right;color:red">' + str + '</div>');
		}
		return data;
	},
	
	redererValue3: function(data, meta, rec, row, col){
		if(!data)
			data = '';
		else {
			str = data >= 0 ? Ext.util.Format.number(data, '0,000.0') + '%' : ('-' + Ext.util.Format.number(-(data), '0,000.0')) + '%';
			data = (data > 0) ? ('<div style="text-align: right">' + str + '</div>') : ('<div style="text-align: right">' + str + '</div>');
		}
		return data;
	},
	
	init: function(typeColumn,typeStore) {
		if (Ext.getCmp('mask') != undefined) 
			Ext.getCmp('mask').show();
		else
		{
			var myMask = new Ext.LoadMask(Ext.getCmp("zone4"), {id: 'mask', msg:Cps.Locale.Zone4.waitMsg});
			myMask.show();
		}
		var columns = [], store, groupers = [], checkGrouper = false;
		if (sessionStorage.groupers != undefined && sessionStorage.groupers != null && sessionStorage.groupers != '')
		{
			groupers = sessionStorage.groupers.split(';');
			checkGrouper = true;
		}
		//build column
        switch (typeColumn)
        {
			case "1":
				columns = [
				{
			        text: Cps.Locale.Zone4.column11,
			        dataIndex: 'PortCode',
					groupHeaderTpl: ['{name:this.formatName}', {
						formatName: function(name) {
							return name != '' ? name : Cps.Locale.Zone4.column11 + ' ' + Cps.Locale.Zone4.define;
						}
					}],
					groupable: true,
			        flex: 1,
			        sortable: true
		        },
				{
			        text: Cps.Locale.Zone4.column12,
			        dataIndex: 'PosCategory',
					groupHeaderTpl: ['{name:this.formatName}', {
						formatName: function(name) {
							return name != '' ? name : Cps.Locale.Zone4.column12 + ' ' + Cps.Locale.Zone4.define;
						}
					}],
					groupable: true,
			        flex: 1,
			        sortable: true
		        },
		        {
			        text: Cps.Locale.Zone4.column6,
			        dataIndex: 'PosInvestmentCode',
					groupHeaderTpl: ['{name:this.formatName}', {
						formatName: function(name) {
							return name != '' ? name : Cps.Locale.Zone4.column6 + ' ' + Cps.Locale.Zone4.define;
						}
					}],
					groupable: true,
			        width: 150,
			        sortable: true
		        },
		        {
			        text: Cps.Locale.Zone4.column1,
			        dataIndex: 'PosName',
					groupHeaderTpl: ['{name:this.formatName}', {
						formatName: function(name) {
							return name != '' ? name : Cps.Locale.Zone4.column1 + ' ' + Cps.Locale.Zone4.define;
						}
					}],
					groupable: true,
			        flex: 1,
			        sortable: true,
					summaryType: 'count',
					summaryRenderer: function(value, summaryData, dataIndex) {
						return (value >= 1 ? '(' + value + ' ' + Cps.Locale.Zone4.records + ')' : '(0 ' + Cps.Locale.Zone4.records +')');
					}
		        },
		        {
			        text: Cps.Locale.Zone4.column2,
			        dataIndex: 'PosCurrency',
					groupHeaderTpl: ['{name:this.formatName}', {
						formatName: function(name) {
							return name != '' ? name : Cps.Locale.Zone4.column2 + ' ' + Cps.Locale.Zone4.define;
						}
					}],
					groupable: true,
					width: 70,
			        sortable: true,
					summaryRenderer: function(value, summaryData, dataIndex) {
						return '<div style="text-align: left;font-weight: bold">Total:</div>';
					}
		        },
                {
                    text: Cps.Locale.Zone4.column3,
			        dataIndex: 'PosQuantity',
					groupable: false,
			        width: 130,
			        sortable: true,
			        renderer: this.redererValue,
					summaryType: 'sum'
                },
		        {
			        text: Cps.Locale.Zone4.column7,
			        dataIndex: 'PosPrice',
					groupable: false,
			        width: 100,
			        sortable: true,
			        renderer: this.redererValue1,
					summaryType: 'sum'
		        },
				{
			        text: Cps.Locale.Zone4.column8,
			        dataIndex: 'PosAmount',
					groupable: false,
			        width: 130,
			        sortable: true,
			        renderer: this.redererValue2,
					summaryType: 'sum'
		        },
				{
			        text: Cps.Locale.Zone4.column4,
			        dataIndex: 'PosMarketPrice',
					groupable: false,
			        width: 100,
			        sortable: true,
			        renderer: this.redererValue1,
					summaryType: 'sum'
		        },
				{
			        text: Cps.Locale.Zone4.column5,
			        dataIndex: 'PosMarketValue',
					groupable: false,
			        width: 130,
			        sortable: true,
			        renderer: this.redererValue2,
					summaryType: 'sum'
		        },
				{
			        text: Cps.Locale.Zone4.column9,
			        dataIndex: 'PosMarketValueRefCur',
					groupable: false,
			        width: 130,
			        sortable: true,
			        renderer: this.redererValue2,
					summaryType: 'sum'
		        },
				{
			        text: Cps.Locale.Zone4.column10,
			        dataIndex: 'PosRepartition',
					groupable: false,
			        width: 65,
			        sortable: true,
					renderer: this.redererValue3
		        }];
				store = Ext.create('Cps.store.PositionByPortfolios');
				if (checkGrouper)
							store = addGroupers(store, groupers);
				store.initData();
				break;
			case "2":
		        columns = [
				{
			        text: Cps.Locale.Zone4.column16,
			        dataIndex: 'Client',
					groupHeaderTpl: ['{name:this.formatName}', {
						formatName: function(name) {
							return name != '' ? name : Cps.Locale.Zone4.column16 + ' ' + Cps.Locale.Zone4.define;
						}
					}],
					groupable: true,
			        flex: 2,
			        sortable: true
		        },
				{
			        text: Cps.Locale.Zone4.column12,
			        dataIndex: 'PosCategory',
					groupHeaderTpl: ['{name:this.formatName}', {
						formatName: function(name) {
							return name != '' ? name : Cps.Locale.Zone4.column12 + ' ' + Cps.Locale.Zone4.define;
						}
					}],
					groupable: true,
			        flex: 1,
			        sortable: true
		        },
		        {
			        text: Cps.Locale.Zone4.column1,
			        dataIndex: 'PosName',
					groupHeaderTpl: ['{name:this.formatName}', {
						formatName: function(name) {
							return name != '' ? name : Cps.Locale.Zone4.column1 + ' ' + Cps.Locale.Zone4.define;
						}
					}],
					groupable: true,
			        flex: 2,
			        sortable: true,
					summaryType: 'count',
					summaryRenderer: function(value, summaryData, dataIndex) {
						return (value >= 1 ? '(' + value + ' ' + Cps.Locale.Zone4.records + ')' : '(0 ' + Cps.Locale.Zone4.records +')');
					}
		        },
		        {
			        text: Cps.Locale.Zone4.column2,
			        dataIndex: 'PosCurrency',
					groupHeaderTpl: ['{name:this.formatName}', {
						formatName: function(name) {
							return name != '' ? name : Cps.Locale.Zone4.column2 + ' ' + Cps.Locale.Zone4.define;
						}
					}],
					groupable: true,
					width: 70,
			        sortable: true,
					summaryRenderer: function(value, summaryData, dataIndex) {
						return '<div style="text-align: left;font-weight: bold">Total:</div>';
					}
		        },
		        {
			        text: Cps.Locale.Zone4.column3,
			        dataIndex: 'PosQuantity',
					groupable: false,
			        width: 150,
			        sortable: true,
			        renderer: this.redererValue,
					summaryType: 'sum'
		        },
                {
                    text: Cps.Locale.Zone4.column4,
			        dataIndex: 'PosMarketPrice',
					groupable: false,
			        width: 150,
			        sortable: true,
			        renderer: this.redererValue1,
					summaryType: 'sum'
                },
		        {
			        text: Cps.Locale.Zone4.column5,
			        dataIndex: 'PosMarketValue',
					groupable: false,
			        width: 150,
			        sortable: true,
			        renderer: this.redererValue2,
					summaryType: 'sum'
		        },
				{
			        text: Cps.Locale.Zone4.column9,
			        dataIndex: 'PosMarketValueRefCur',
					groupable: false,
			        width: 150,
			        sortable: true,
			        renderer: this.redererValue2,
					summaryType: 'sum'
		        }];
				store = Ext.create('Cps.store.PositionByClients');
				if (checkGrouper)
							store = addGroupers(store, groupers);
				store.initData();
                break;
            case "5":
				var column1 = {
						text: Cps.Locale.Zone4.column13,
						dataIndex: 'PosBank',
						groupHeaderTpl: ['{name:this.formatName}', {
							formatName: function(name) {
								return name != '' ? name : Cps.Locale.Zone4.column13 + ' ' + Cps.Locale.Zone4.define;
							}
						}],
						groupable: true,
						flex: 2,
						sortable: true
					},
					column2 = {
						text: Cps.Locale.Zone4.column14,
						dataIndex: 'PosManager',
						groupHeaderTpl: ['{name:this.formatName}', {
							formatName: function(name) {
								return name != '' ? name : Cps.Locale.Zone4.column14 + ' ' + Cps.Locale.Zone4.define;
							}
						}],
						groupable: true,
						flex: 2,
						sortable: true
					}
		        columns = [
				{
			        text: Cps.Locale.Zone4.column12,
			        dataIndex: 'PosCategory',
					groupHeaderTpl: ['{name:this.formatName}', {
						formatName: function(name) {
							return name != '' ? name : Cps.Locale.Zone4.column12 + ' ' + Cps.Locale.Zone4.define;
						}
					}],
					groupable: true,
			        flex: 1,
			        sortable: true
		        },
		        {
			        text: Cps.Locale.Zone4.column1,
			        dataIndex: 'PosName',
					groupHeaderTpl: ['{name:this.formatName}', {
						formatName: function(name) {
							return name != '' ? name : Cps.Locale.Zone4.column1 + ' ' + Cps.Locale.Zone4.define;
						}
					}],
					groupable: true,
			        flex: 2,
			        sortable: true,
					summaryType: 'count',
					summaryRenderer: function(value, summaryData, dataIndex) {
						return (value >= 1 ? '(' + value + ' ' + Cps.Locale.Zone4.records + ')' : '(0 ' + Cps.Locale.Zone4.records +')');
					}
		        },
		        {
			        text: Cps.Locale.Zone4.column2,
			        dataIndex: 'PosCurrency',
					groupHeaderTpl: ['{name:this.formatName}', {
						formatName: function(name) {
							return name != '' ? name : Cps.Locale.Zone4.column2 + ' ' + Cps.Locale.Zone4.define;
						}
					}],
					groupable: true,
			        width: 70,
			        sortable: true,
					summaryRenderer: function(value, summaryData, dataIndex) {
						return '<div style="text-align: left;font-weight: bold">Total:</div>';
					}
		        },
		        {
			        text: Cps.Locale.Zone4.column3,
			        dataIndex: 'PosQuantity',
					groupable: false,
			        width: 180,
			        sortable: true,
			        renderer: this.redererValue,
					summaryType: 'sum'
		        },
                {
                    text: Cps.Locale.Zone4.column4,
			        dataIndex: 'PosMarketPrice',
					groupable: false,
			        width: 180,
			        sortable: true,
			        renderer: this.redererValue1,
					summaryType: 'sum'
                },
		        {
			        text: Cps.Locale.Zone4.column5,
			        dataIndex: 'PosMarketValue',
					groupable: false,
			        width: 180,
			        sortable: true,
			        renderer: this.redererValue2,
					summaryType: 'sum'
		        }];
				switch (typeStore)
				{
					case "1":
						columns.unshift(column1);
						store = Ext.create('Cps.store.PositionByDepositarys');
						if (checkGrouper)
							store = addGroupers(store, groupers);
						store.initData();
						break;
					case "2":
						columns.unshift(column2);
						store = Ext.create('Cps.store.PositionByManagers');
						if (checkGrouper)
							store = addGroupers(store, groupers);
						store.initData();
						break;
					case "3":
						store = Ext.create('Cps.store.AllPositions');
						if (checkGrouper)
							store = addGroupers(store, groupers);
						store.initData();
						break;
				}

                break;
        }
		var grpl= Ext.create('Ext.ux.grid.plugin.GroupingPanel');
		//do render data
		var gridNew = Ext.create('Ext.grid.Panel', {
			//id: 'gridZone4',
			columns: columns,
			store: store,
			overflowY: 'auto',
			features: [Ext.create('Ext.ux.grid.feature.MultiGroupingSummary', {
				id: 'group',
				hideGroupedHeader: true,
				enableGroupingMenu: true,
				startCollapsed: false
			}), {
				ftype:  'summary',
				dock:   'bottom'
			}],
			plugins: [
				grpl
			],
			listeners: {
				columnschanged: function(){
					var menu = Ext.ComponentQuery.query('grid')[0].headerCt.getMenu();
					menu.add(
						[{
						text: Cps.Locale.Zone4.showGroupingPanel,
						xtype: 'menucheckitem',
						id: 'toggleHeaderPanel',
						handler: function() {
							if(grpl.disabled)
								grpl.enable();
							else
								grpl.disable();
						},
						listeners:{
							beforerender: function(){
								this.checked= grpl.disable;
							}
						}
          	}]
					);           
				}
      },
			border: 0
		});
		this.getPanel().insert(0, gridNew);
		//grid.reconfigure(store,columns);
	},
	initChart: function(typeChart, node) {
		var pn = this.getPanel(), combobox = Ext.getCmp('cbb'), combobox1 = Ext.getCmp('cbb1'), chartCtn = Ext.getCmp('chartCtn');
		if (Ext.getCmp('mask') != undefined) 
			Ext.getCmp('mask').show();
		else
		{
			var myMask = new Ext.LoadMask(pn, {id: 'mask', msg: Cps.Locale.Zone4.waitMsg});
			myMask.show();
		}
		if (chartCtn != undefined)
			chartCtn.destroy();
		switch (combobox.getValue())
		{
			case "1":
				var container = Ext.create('Ext.container.Container', {
					id: 'chartCtn',
					autoScroll: true,
					layout: {
						type: 'column'
					},
					items: [
						{
							xtype: 'container',
							columnWidth: .5,
							defaults: {
								border: 1,
								height: 300
							}
						},
						{
							xtype: 'container',
							columnWidth: .5,
							defaults: {
								border: 1,
								height: 300
							}							
						}
					]
				});
				Ext.Ajax.request({
					method: 'GET',
					url: Cps.Config.Api.getPositionByPortfolio,
					params: getParamPosition,
					success: function(data) {
						if (IsJson(data.responseText))
						{
							var data1 = [], f_data = [];
							data = Ext.JSON.decode(data.responseText).data;
							data = data ? data.dsPositionByPortfolio : null;
							data1 = data ? data.ttPortfolio : null;
							data = data ? data.ttPositionByPort : null;
							if (data != undefined)
							{
								if (data.length != undefined)
								{
									for(i = 0; i < data.length; i++) {
										for(j = 0; j < data1.length; j++)
										{
											if (data1[j].PortCode == data[i].PortCode)
											{
												data[i].PortCode = data1[j].PortName + ' - ' + data1[j].PortCurrency + ' ' + data1[j].PortTotal;
												f_data.push(data[i]);
												break;
											}
										}
									}
								}
								else
									f_data.push(data);
							}
							var f_data = groupPortfolio(f_data);
							if (node == 1)
							{
								for(var i = 0; i < f_data.length; i++)
								{
									var title = f_data[i][0] != undefined ? f_data[i][0].PortCode : '',
										cur = distinctObj(f_data[i], 'PosCurrency'), 
										store = Ext.create('Ext.data.Store',{
											fields: ['obj','value'],
											data: sumPrice(f_data[i], cur, 1),
											counter: 0
										}),
										chart = Ext.create('Ext.panel.Panel', {
											title: title,
											layout: 'fit',
											items: [ Ext.create(typeChart, {
												store: store
											})]
										});
									if ( i % 2 == 0)
										container.items.items[0].insert(0,chart);
									else
										container.items.items[1].insert(0,chart);
								}
							}
							else if (node == 2)
							{
								for(var i = 0; i < f_data.length; i++)
								{
									var title = f_data[i][0] != undefined ? f_data[i][0].PortCode : '',
										cur = distinctObj(f_data[i], 'PosCategory'), 
										store = Ext.create('Ext.data.Store',{
											fields: ['obj','value'],
											data: sumPrice(f_data[i], cur, 2),
											counter: 0
										}),
										chart = Ext.create('Ext.panel.Panel', {
											title: title,
											layout: 'fit',
											items: [ Ext.create(typeChart, {
												store: store
											})]
										});
									if ( i % 2 == 0)
										container.items.items[0].insert(0,chart);
									else
										container.items.items[1].insert(0,chart);
								}
							}
							else if (node == 3)
							{
								for(var i = 0; i < f_data.length; i++)
								{
									var title = f_data[i][0] != undefined ? f_data[i][0].PortCode : '',
										cur = distinctObj(f_data[i], 'PosDepository'), 
										store = Ext.create('Ext.data.Store',{
											fields: ['obj','value'],
											data: sumPrice(f_data[i], cur, 3),
											counter: 0
										}),
										chart = Ext.create('Ext.panel.Panel', {
											title: title,
											layout: 'fit',
											items: [ Ext.create(typeChart, {
												store: store
											})]
										});
									if ( i % 2 == 0)
										container.items.items[0].insert(0,chart);
									else
										container.items.items[1].insert(0,chart);
								}
							}
							pn.insert(1, container);
							if (combobox1.ownerCt.items.items[0].text != undefined)
								width = textWidth(combobox1.ownerCt.items.items[0].text) + 20;
							else
								width = textWidth(combobox1.ownerCt.items.items[0].textEl.dom.innerText) + 20;
							combobox1.el.setStyle('left',width + 'px');
							if (Ext.getCmp('mask') != undefined) 
								Ext.getCmp('mask').hide()
						}
					},
					scope: this
				});
				break;
			case "2":
				var container = Ext.create('Ext.container.Container', {
					id: 'chartCtn',
					autoScroll: true,
					layout: {
						type: 'column'
					},
					items: [
						{
							xtype: 'container',
							columnWidth: .5,
							defaults: {
								border: 1,
								height: 300
							}
						},
						{
							xtype: 'container',
							columnWidth: .5,
							defaults: {
								border: 1,
								height: 300
							}
						}
					]
				});
				Ext.Ajax.request({
					method: 'GET',
					url: Cps.Config.Api.getPositionByPortfolio,
					params: getParamPosition,
					success: function(data) {
						if (IsJson(data.responseText))
						{
							var data1 = [], f_data = [];
							data = Ext.JSON.decode(data.responseText).data;
							data = data ? data.dsPositionByPortfolio : null;
							data1 = data ? data.ttPortfolio : null;
							data = data ? data.ttPositionByPort : null;
							if (data != undefined)
							{
								if (data.length != undefined)
								{
									for(i = 0; i < data.length; i++) {
										for(j = 0; j < data1.length; j++)
										{
											if (data1[j].PortCode == data[i].PortCode)
											{
												data[i].PortCode = data1[j].PortName + ' - ' + data1[j].PortCurrency + ' ' + data1[j].PortTotal;
												f_data.push(data[i]);
												break;
											}
										}
									}
								}
								else
									f_data.push(data);
							}
							var f_data = groupPortfolio(f_data);
							if (node == 1)
							{
								for(var i = 0; i < f_data.length; i++)
								{
									var title = f_data[i][0] != undefined ? f_data[i][0].PortCode : '',
										cur = distinctObj(f_data[i], 'PosCurrency'), 
										store = Ext.create('Ext.data.Store',{
											fields: ['obj','value'],
											data: sumPrice(f_data[i], cur, 1),
											counter: 0
										}),
										chart = Ext.create('Ext.panel.Panel', {
											title: title,
											layout: 'fit',
											items: [ Ext.create(typeChart, {
												store: store
											})]
										});
									if ( i % 2 == 0)
										container.items.items[0].insert(0,chart);
									else
										container.items.items[1].insert(0,chart);
								}
							}
							else if (node == 2)
							{
								for(var i = 0; i < f_data.length; i++)
								{
									var title = f_data[i][0] != undefined ? f_data[i][0].PortCode : '',
										cur = distinctObj(f_data[i], 'PosCategory'), 
										store = Ext.create('Ext.data.Store',{
											fields: ['obj','value'],
											data: sumPrice(f_data[i], cur, 2),
											counter: 0
										}),
										chart = Ext.create('Ext.panel.Panel', {
											title: title,
											layout: 'fit',
											items: [ Ext.create(typeChart, {
												store: store
											})]
										});
									if ( i % 2 == 0)
										container.items.items[0].insert(0,chart);
									else
										container.items.items[1].insert(0,chart);
								}
							}
							else if (node == 3)
							{
								for(var i = 0; i < f_data.length; i++)
								{
									var title = f_data[i][0] != undefined ? f_data[i][0].PortCode : '',
										cur = distinctObj(f_data[i], 'PosDepository'), 
										store = Ext.create('Ext.data.Store',{
											fields: ['obj','value'],
											data: sumPrice(f_data[i], cur, 3),
											counter: 0
										}),
										chart = Ext.create('Ext.panel.Panel', {
											title: title,
											layout: 'fit',
											items: [ Ext.create(typeChart, {
												store: store
											})]
										});
									if ( i % 2 == 0)
										container.items.items[0].insert(0,chart);
									else
										container.items.items[1].insert(0,chart);
								}
							}
							pn.insert(1, container);
							if (combobox1.ownerCt.items.items[0].text != undefined)
								width = textWidth(combobox1.ownerCt.items.items[0].text) + 20;
							else
								width = textWidth(combobox1.ownerCt.items.items[0].textEl.dom.innerText) + 20;
							combobox1.el.setStyle('left',width + 'px');
							if (Ext.getCmp('mask') != undefined) 
								Ext.getCmp('mask').hide()
						}
					},
					scope: this
				});
				break;
			case "3":
				var container = Ext.create('Ext.container.Container', {
					id: 'chartCtn',
					autoScroll: true,
					layout: {
						type: 'column'
					},
					items: [
						{
							xtype: 'container',
							columnWidth: .5,
							defaults: {
								border: 1,
								height: 300
							}
						},
						{
							xtype: 'container',
							columnWidth: .5,
							defaults: {
								border: 1,
								height: 300
							}
						}
					]
				});
				Ext.Ajax.request({
					method: 'GET',
					url: Cps.Config.Api.getPositionByPortfolio,
					params: getParamPosition,
					success: function(data) {
						if (IsJson(data.responseText))
						{
							var data1 = [], f_data = [];
							data = Ext.JSON.decode(data.responseText).data;
							data = data ? data.dsPositionByPortfolio : null;
							data1 = data ? data.ttPortfolio : null;
							data = data ? data.ttPositionByPort : null;
							if (data != undefined)
							{
								if (data.length != undefined)
								{
									for(i = 0; i < data.length; i++) {
										for(j = 0; j < data1.length; j++)
										{
											if (data1[j].PortCode == data[i].PortCode)
											{
												data[i].PortCode = data1[j].PortName + ' - ' + data1[j].PortCurrency + ' ' + data1[j].PortTotal;
												f_data.push(data[i]);
												break;
											}
										}
									}
								}
								else
									f_data.push(data);
							}
							var f_data = groupPortfolio(f_data);
							if (node == 1)
							{
								for(var i = 0; i < f_data.length; i++)
								{
									var title = f_data[i][0] != undefined ? f_data[i][0].PortCode : '',
										cur = distinctObj(f_data[i], 'PosCurrency'), 
										store = Ext.create('Ext.data.Store',{
											fields: ['obj','value'],
											data: sumPrice(f_data[i], cur, 1),
											counter: 0
										}),
										chart = Ext.create('Ext.panel.Panel', {
											title: title,
											layout: 'fit',
											items: [ Ext.create(typeChart, {
												store: store
											})]
										});
									if ( i % 2 == 0)
										container.items.items[0].insert(0,chart);
									else
										container.items.items[1].insert(0,chart);
								}
							}
							else if (node == 2)
							{
								for(var i = 0; i < f_data.length; i++)
								{
									var title = f_data[i][0] != undefined ? f_data[i][0].PortCode : '',
										cur = distinctObj(f_data[i], 'PosCategory'), 
										store = Ext.create('Ext.data.Store',{
											fields: ['obj','value'],
											data: sumPrice(f_data[i], cur, 2),
											counter: 0
										}),
										chart = Ext.create('Ext.panel.Panel', {
											title: title,
											layout: 'fit',
											items: [ Ext.create(typeChart, {
												store: store
											})]
										});
									if ( i % 2 == 0)
										container.items.items[0].insert(0,chart);
									else
										container.items.items[1].insert(0,chart);
								}
							}
							else if (node == 3)
							{
								for(var i = 0; i < f_data.length; i++)
								{
									var title = f_data[i][0] != undefined ? f_data[i][0].PortCode : '',
										cur = distinctObj(f_data[i], 'PosDepository'), 
										store = Ext.create('Ext.data.Store',{
											fields: ['obj','value'],
											data: sumPrice(f_data[i], cur, 3),
											counter: 0
										}),
										chart = Ext.create('Ext.panel.Panel', {
											title: title,
											layout: 'fit',
											items: [ Ext.create(typeChart, {
												store: store
											})]
										});
									if ( i % 2 == 0)
										container.items.items[0].insert(0,chart);
									else
										container.items.items[1].insert(0,chart);
								}
							}
							pn.insert(1, container);
							if (combobox1.ownerCt.items.items[0].text != undefined)
								width = textWidth(combobox1.ownerCt.items.items[0].text) + 20;
							else
								width = textWidth(combobox1.ownerCt.items.items[0].textEl.dom.innerText) + 20;
							combobox1.el.setStyle('left',width + 'px');
							if (Ext.getCmp('mask') != undefined) 
								Ext.getCmp('mask').hide()
						}
					},
					scope: this
				});
				break;
			case "4":
				var container = Ext.create('Ext.container.Container', {
					id: 'chartCtn',
					autoScroll: true,
					layout: {
						type: 'column'
					},
					items: [
						{
							xtype: 'container',
							columnWidth: .5,
							defaults: {
								border: 1,
								height: 300
							}
						},
						{
							xtype: 'container',
							columnWidth: .5,
							defaults: {
								border: 1,
								height: 300
							}
						}
					]
				});
				Ext.Ajax.request({
					method: 'GET',
					url: Cps.Config.Api.getPositionByPortfolio,
					params: getParamPosition,
					success: function(data) {
						if (IsJson(data.responseText))
						{
							var data1 = [], f_data = [];
							data = Ext.JSON.decode(data.responseText).data;
							data = data ? data.dsPositionByPortfolio : null;
							data1 = data ? data.ttPortfolio : null;
							data = data ? data.ttPositionByPort : null;
							if (data != undefined)
							{
								if (data.length != undefined)
								{
									for(i = 0; i < data.length; i++) {
										for(j = 0; j < data1.length; j++)
										{
											if (data1[j].PortCode == data[i].PortCode)
											{
												data[i].PortCode = data1[j].PortName + ' - ' + data1[j].PortCurrency + ' ' + data1[j].PortTotal;
												f_data.push(data[i]);
												break;
											}
										}
									}
								}
								else
									f_data.push(data);
							}
							var f_data = groupPortfolio(f_data);
							if (node == 1)
							{
								for(var i = 0; i < f_data.length; i++)
								{
									var title = f_data[i][0] != undefined ? f_data[i][0].PortCode : '',
										cur = distinctObj(f_data[i], 'PosCurrency'), 
										store = Ext.create('Ext.data.Store',{
											fields: ['obj','value'],
											data: sumPrice(f_data[i], cur, 1),
											counter: 0
										}),
										chart = Ext.create('Ext.panel.Panel', {
											title: title,
											layout: 'fit',
											items: [ Ext.create(typeChart, {
												store: store
											})]
										});
									if ( i % 2 == 0)
										container.items.items[0].insert(0,chart);
									else
										container.items.items[1].insert(0,chart);
								}
							}
							else if (node == 2)
							{
								for(var i = 0; i < f_data.length; i++)
								{
									var title = f_data[i][0] != undefined ? f_data[i][0].PortCode : '',
										cur = distinctObj(f_data[i], 'PosCategory'), 
										store = Ext.create('Ext.data.Store',{
											fields: ['obj','value'],
											data: sumPrice(f_data[i], cur, 2),
											counter: 0
										}),
										chart = Ext.create('Ext.panel.Panel', {
											title: title,
											layout: 'fit',
											items: [ Ext.create(typeChart, {
												store: store
											})]
										});
									if ( i % 2 == 0)
										container.items.items[0].insert(0,chart);
									else
										container.items.items[1].insert(0,chart);
								}
							}
							else if (node == 3)
							{
								for(var i = 0; i < f_data.length; i++)
								{
									var title = f_data[i][0] != undefined ? f_data[i][0].PortCode : '',
										cur = distinctObj(f_data[i], 'PosDepository'), 
										store = Ext.create('Ext.data.Store',{
											fields: ['obj','value'],
											data: sumPrice(f_data[i], cur, 3),
											counter: 0
										}),
										chart = Ext.create('Ext.panel.Panel', {
											title: title,
											layout: 'fit',
											items: [ Ext.create(typeChart, {
												store: store
											})]
										});
									if ( i % 2 == 0)
										container.items.items[0].insert(0,chart);
									else
										container.items.items[1].insert(0,chart);
								}
							}
							pn.insert(1, container);
							if (combobox1.ownerCt.items.items[0].text != undefined)
								width = textWidth(combobox1.ownerCt.items.items[0].text) + 20;
							else
								width = textWidth(combobox1.ownerCt.items.items[0].textEl.dom.innerText) + 20;
							combobox1.el.setStyle('left',width + 'px');
							if (Ext.getCmp('mask') != undefined) 
								Ext.getCmp('mask').hide()
						}
					},
					scope: this
				});
				break;
			case "5":
				var container = Ext.create('Ext.container.Container', {
					id: 'chartCtn',
					layout: {
						type: 'fit'
					}
				}), f_data = [];
				Ext.Ajax.request({
					method: 'GET',
					url: Cps.Config.Api.getAllPosition,
					params: getParamPosition,
					success: function(data) {
						if (IsJson(data.responseText))
						{
							data = Ext.JSON.decode(data.responseText).data;
							data = data ? data.ttAllPosition : null;
							data = data ? data.ttAllPositionRow : null;
							if (data != undefined)
							{
								if (data.length != undefined)
								{
									for(i = 0; i < data.length; i++) {
										f_data.push(data[i]);
									}
								}
								else
									f_data.push(data);
							}
							if (node == 1)
							{
								var cur = distinctObj(f_data, 'PosCurrency'),
									store = Ext.create('Ext.data.Store',{
										fields: ['obj','value'],
										data: sumPrice(f_data, cur, 1),
										counter: 0
									});
							}
							else if (node == 2)
							{
								var cur = distinctObj(f_data,'PosCategory'), 
									store = Ext.create('Ext.data.Store',{
										fields: ['obj','value'],
										data: sumPrice(f_data, cur, 2),
										counter: 0
									});
							}
							else if (node == 3)
							{
								var cur = distinctObj(f_data,'PosDepository'), 
									store = Ext.create('Ext.data.Store',{
										fields: ['obj','value'],
										data: sumPrice(f_data, cur, 1),
										counter: 0
									});
							}
							var	chart = Ext.create(typeChart, {
								store: store
							});
							container.insert(0,chart);
							pn.insert(1, container);
							if (combobox1.ownerCt.items.items[0].text != undefined)
								width = textWidth(combobox1.ownerCt.items.items[0].text) + 20;
							else
								width = textWidth(combobox1.ownerCt.items.items[0].textEl.dom.innerText) + 20;
							combobox1.el.setStyle('left',width + 'px');
							if (Ext.getCmp('mask') != undefined) 
								Ext.getCmp('mask').hide()
						}
					},
					scope: this
				});
				break;
		}
	}
});
function addGroupers(store, groupers) {
	store.groupers.removeAll();
	for (var i = 0; i < groupers.length; i++)
	{
		var group = Ext.create('Ext.util.Grouper', {
			direction: groupers[i].split(',')[1],
			property: groupers[i].split(',')[0],
			root: "data"
		});
		store.groupers.add(group);
	}
	return store;
};